package com.itestor.iptv.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import org.apache.log4j.Logger;

/**
 * 日期格式工具类.
 */
public class DateUtil {
    /**
     * logger.
     */
    private static final Logger logger = Logger.getLogger(DateUtil.class);

    /**
     * date format.
     */
    private static final SimpleDateFormat sdf = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss", Locale.US);

    public static final long MS_SECOND = 1000;
    public static final long MS_MINUTE = 60 * MS_SECOND;
    public static final long MS_HOUR = 60 * MS_MINUTE;
    public static final long MS_DAY = 24 * MS_HOUR;

    public static long getTimeBySecond(long time) {
        return time / MS_SECOND * MS_SECOND;
    }

    public static long getTimeByMinute(long time) {
        return time / MS_MINUTE * MS_MINUTE;
    }

    public static long getTimeByHour(long time) {
        return time / MS_HOUR * MS_HOUR;
    }

    public static long getTimeByDay(long time) {
        return time / MS_DAY * MS_DAY;
    }

    /**
     * String转成Date类型.
     * @param source 源
     * @param pattern 格式
     * @return Date
     * @throws ParseException ParseException
     */
    public static Date stringToDate(String source, String pattern)
            throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
        Date date = null;
        date = sdf.parse(source);
        return date;
    }

    /**
     * String转成Date类型.
     * @param source 源
     * @param pattern 格式
     * @return Date
     */
    public static Date convertToDate(String source, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
        Date date = null;
        try {
            date = sdf.parse(source);
        } catch (ParseException e) {
            logger.error(source + "--转换日期类型异常." + e.getMessage());
        }
        return date;
    }

    /**
     * 日期类型转成String.
     * @param date date
     * @param pattern pattern
     * @return String
     */
    public static String convertToString(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
        String dateStr = null;
        if (date != null) {
            dateStr = sdf.format(date);
        }
        return dateStr;
    }

    /**
     * 返回之前hour小时的时间.
     * @param nowTime nowTime
     * @param hour hour
     * @return Date Date
     */
    public static Date beforHour(Date nowTime, int hour) {
        Date beforeTime = null;
        if (nowTime != null) {
            Calendar ca = Calendar.getInstance();
            ca.setTime(nowTime);
            ca.set(Calendar.HOUR_OF_DAY, ca.get(Calendar.HOUR_OF_DAY) - hour);
            beforeTime = ca.getTime();
        }
        return beforeTime;
    }

    /**
     * 转成以秒为时间位单的数字.
     * @param source source
     * @param pattern pattern
     * @return Long
     * @throws ParseException ParseException
     */
    public static Long dateToSecTime(String source, String pattern)
            throws ParseException {
        Date d = stringToDate(source, pattern);
        return d.getTime() / 1000;
    }

    /**
     * 取天的开始时间.
     * @param date 日期
     * @return Date
     */
    public static Date getStartTimeByDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 取天的结束时间.
     * @param date 日期
     * @return Date
     */
    public static Date getEndTimeByDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);

        return calendar.getTime();
    }

    /**
     * 获取n天前的时间.
     * @param before int.
     * @return date.
     */
    public static Date getLastDate(int before) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)
                - before);
        return calendar.getTime();
    }

    /**
     * 获取数据归并的时间段.
     * @return List.
     */
    public static List<Date> getTriggerLastDaySection() {
        List<Date> datelist = new ArrayList<Date>();
        Calendar cld1 = Calendar.getInstance();
        Calendar start = cld1;
        start.set(Calendar.DAY_OF_MONTH, cld1.get(Calendar.DAY_OF_MONTH) - 1);
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        start.set(Calendar.MILLISECOND, 0);

        Calendar cld2 = Calendar.getInstance();
        Calendar end = cld2;
        end.set(Calendar.DAY_OF_MONTH, cld2.get(Calendar.DAY_OF_MONTH) - 1);
        end.set(Calendar.HOUR_OF_DAY, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        end.set(Calendar.MILLISECOND, 59);

        datelist.add(start.getTime());
        datelist.add(end.getTime());
        return datelist;
    }

    /**
     * 获取日报时间
     * @return List<Date>
     */
    public static List<Date> getLastDay() {
        List<Date> datelist = new ArrayList<Date>();
        Calendar calendar = Calendar.getInstance();
        Date date_end = calendar.getTime();
        Calendar start = calendar;
        start.set(Calendar.DAY_OF_MONTH,
                calendar.get(Calendar.DAY_OF_MONTH) - 1);

        datelist.add(date_end);
        datelist.add(new Date(calendar.getTimeInMillis()));
        return datelist;
    }

    /**
     * 获取周报时间
     * @return List<Date>
     */
    public static List<Date> getLastWeek() {
        List<Date> datelist = new ArrayList<Date>();
        Calendar calendar = Calendar.getInstance();
        calendar
                .set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
        Date date_end = calendar.getTime();
        calendar.set(Calendar.WEEK_OF_YEAR,
                calendar.get(Calendar.WEEK_OF_YEAR) - 1);
        Date date_begin = calendar.getTime();
        datelist.add(date_end);
        datelist.add(date_begin);
        return datelist;
    }

    /**
     * 获取月报时间
     * @return List<Date>
     */
    public static List<Date> getLastMonth() {
        List<Date> datelist = new ArrayList<Date>();
        Calendar ca = Calendar.getInstance();
        Date month_end = ca.getTime();
        ca.set(Calendar.MONTH, ca.get(Calendar.MONTH) - 1);
        ca.set(Calendar.DAY_OF_MONTH, ca.get(Calendar.DAY_OF_MONTH) - 2);
        Date month_begin = ca.getTime();
        datelist.add(month_end);
        datelist.add(month_begin);
        return datelist;
    }

    /**
     * 获取年报时间
     * @return List<Date>
     */
    public static List<Date> getLastYear() {
        List<Date> datelist = new ArrayList<Date>();
        Calendar ca = Calendar.getInstance();
        Date year_end = ca.getTime();
        ca.set(Calendar.YEAR, ca.get(Calendar.YEAR) - 1);
        ca.set(Calendar.DAY_OF_MONTH, ca.get(Calendar.DAY_OF_MONTH) - 5);
        Date year_begin = ca.getTime();
        datelist.add(year_end);
        datelist.add(year_begin);
        return datelist;
    }

    /**
     * 获取活跃度周报时间
     * @return List<Date>
     */
    public static List<Date> getActiveWeeklyDateList() {
        List<Date> datelist = new ArrayList<Date>();
        Calendar start = Calendar.getInstance();
        start
                .set(Calendar.WEEK_OF_MONTH,
                        start.get(Calendar.WEEK_OF_MONTH) - 1);
        start.set(Calendar.DAY_OF_WEEK, 1);
        start.set(Calendar.DAY_OF_YEAR, start.get(Calendar.DAY_OF_YEAR) + 1);
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        start.set(Calendar.MILLISECOND, 0);

        Calendar end = Calendar.getInstance();
        end.set(Calendar.WEEK_OF_MONTH, end.get(Calendar.WEEK_OF_MONTH) - 1);
        end.set(Calendar.DAY_OF_WEEK, 7);
        end.set(Calendar.DAY_OF_YEAR, end.get(Calendar.DAY_OF_YEAR) + 1);
        end.set(Calendar.HOUR_OF_DAY, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        end.set(Calendar.MILLISECOND, 59);

        Date s = start.getTime();
        Date e = end.getTime();

        datelist.add(s);
        datelist.add(e);
        return datelist;
    }

    /**
     * 取上月的开始时间.
     * @param date
     * @return Date
     */
    public static Date getStartDateByLastMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 取上月的结束时间.
     * @param date
     * @return Date
     */
    public static Date getEndDateByLastMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 组合日期.
     * @param year
     * @param month
     * @return Date
     */
    public static Date getDateByString(String year, String month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取月报的开始结束时间.
     * @param date
     * @return
     */
    public static List<Date> getMonthlyDateList(Date date) {
        List<Date> list = new ArrayList<Date>();

        Calendar s_cal = Calendar.getInstance();
        s_cal.setTime(date);
        s_cal.set(Calendar.DAY_OF_MONTH, 1);
        s_cal.set(Calendar.HOUR_OF_DAY, 0);
        s_cal.set(Calendar.MINUTE, 0);
        s_cal.set(Calendar.SECOND, 0);
        s_cal.set(Calendar.MILLISECOND, 0);

        Calendar e_cal = Calendar.getInstance();
        e_cal.setTime(date);
        e_cal.set(Calendar.DAY_OF_MONTH, s_cal
                .getActualMaximum(Calendar.DAY_OF_MONTH));
        e_cal.set(Calendar.HOUR_OF_DAY, 23);
        e_cal.set(Calendar.MINUTE, 59);
        e_cal.set(Calendar.SECOND, 59);
        e_cal.set(Calendar.MILLISECOND, 59);

        list.add(s_cal.getTime());
        list.add(e_cal.getTime());
        return list;
    }

    /**
     * 将日期（String形式）按格式转化成日期（Date形式）.
     * @param dateStr 日期（String形式）
     * @param formatStr 日期格式
     * @return date 日期（Date形式）
     */
    public static Date getParseDate(String dateStr, String formatStr) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(formatStr, Locale.UK);
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.getStackTrace();
        }
        return date;
    }

    /**
     * 月的第一天.
     * @param date 日期
     * @return Date
     */
    public static Date getFirstDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        calendar.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        calendar.set(Calendar.DATE, 1);
        return calendar.getTime();
    }

    /**
     * 月的最后一天.
     * @param date 日期
     * @return Date
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day = 0;
        int month = cal.get(Calendar.MONTH) + 1;
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
                || month == 10 || month == 12) {
            day = 31;
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            day = 30;
        } else if (month == 2) {
            if (((GregorianCalendar) cal).isLeapYear(cal.get(Calendar.YEAR))) {
                day = 29;
            } else {
                day = 28;
            }
        } else {
            day = -1;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        calendar.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        calendar.set(Calendar.DATE, day);
        return calendar.getTime();
    }

    public static void main(String[] args) throws ParseException {

    }
}
